package com.parachutes.aspmonitorcore.controller;


import com.parachutes.aspmonitorapi.service.EmailService;
import com.parachutes.aspmonitorcore.domian.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Random;

/**
 * <p>
 * 邮箱
 * </p>
 *
 * @author machi
 * @date 2025/1/22 16:52
 */
@Slf4j
@RestController
@RequestMapping("/email")
public class EmailController {
    @Autowired
    private EmailService emailService;

    @GetMapping("/send")
    public Result<String> sendEmail(String email) throws Exception {
        //1.创建一封邮件
        MimeMessage message = new MimeMessage(emailService.getSession());

        //2.from:发件人

        //3.to:收件人
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(email, "User", "UTF-8"));

        //4.邮件主题
        message.setSubject("【API分享平台】注册短信验证码", "UTF-8");

        //5.邮件主题
        Random random = new Random();
        int code = random.nextInt(999999);
        message.setContent("<html><body><h1>注册短信验证码</h1><p>您的验证码为："+code+"</p></body></html>", "text/html;charset=UTF-8");

        //6.设置发件时间
        message.setSentDate(new Date());

        //7.保存设置
        message.saveChanges();
        emailService.send(message);
        log.info("邮件发送成功");
        return Result.of(true, "邮件发送成功", "邮件发送成功");
    }
}
